@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

echo.
echo LongImageReader 自动构建脚本
echo ============================================
echo.

set "CMAKE_BUILD_TYPE=Release"
echo [OK] 自动选择 Release 版本构建
echo.

:: 检查项目文件
if not exist "CMakeLists.txt" (
    echo [ERROR] 错误: 未找到 CMakeLists.txt 文件
    echo 请在项目根目录运行此脚本
    exit /b 1
)

:: 设置 Qt 路径 - 支持更多版本
set "QT_VERSIONS=6.9.2 6.8.1 6.8.0 6.7.0 6.6.0"
set "QT_PATH="

echo 正在搜索 Qt 安装...
for %%v in (%QT_VERSIONS%) do (
    if exist "C:\Qt\%%v\mingw_64" (
        set "QT_PATH=C:\Qt\%%v\mingw_64"
        echo [OK] 找到 Qt %%v 安装: !QT_PATH!
        goto :qt_found
    )
    if exist "C:\Qt\%%v\msvc2019_64" (
        set "QT_PATH=C:\Qt\%%v\msvc2019_64"
        echo [OK] 找到 Qt %%v MSVC 安装: !QT_PATH!
        goto :qt_found
    )
)

:qt_found
if "%QT_PATH%"=="" (
    echo [ERROR] 错误: 未找到 Qt 安装
    echo 请确保 Qt 安装在 C:\Qt\ 目录下
    echo 支持的版本: 6.6.0, 6.7.0, 6.8.0, 6.8.1, 6.9.2
    exit /b 1
)

:: 设置工具路径 - 支持多种可能的安装位置
set "CMAKE_PATH="
set "MINGW_PATH="

echo 检查构建工具...

:: 检查 CMake - 支持多种安装位置
if exist "C:\Qt\Tools\CMake_64\bin" (
    set "CMAKE_PATH=C:\Qt\Tools\CMake_64\bin"
    echo [OK] 找到 Qt CMake: !CMAKE_PATH!
) else if exist "C:\Program Files\CMake\bin" (
    set "CMAKE_PATH=C:\Program Files\CMake\bin"
    echo [OK] 找到系统 CMake: !CMAKE_PATH!
) else if exist "C:\Program Files (x86)\CMake\bin" (
    set "CMAKE_PATH=C:\Program Files (x86)\CMake\bin"
    echo [OK] 找到系统 CMake: !CMAKE_PATH!
) else (
    echo [WARN]  未找到 CMake，尝试使用系统 PATH
)

:: 检查 MinGW - 支持多种版本
for %%v in (1310 1120 810 730) do (
    if exist "C:\Qt\Tools\mingw%%v_64\bin" (
        set "MINGW_PATH=C:\Qt\Tools\mingw%%v_64\bin"
        echo [OK] 找到 Qt MinGW %%v: !MINGW_PATH!
        goto :mingw_found
    )
)

:: 检查系统 MinGW
if exist "C:\mingw64\bin" (
    set "MINGW_PATH=C:\mingw64\bin"
    echo [OK] 找到系统 MinGW: !MINGW_PATH!
) else (
    echo [WARN]  未找到 MinGW，尝试使用系统 PATH
)

:mingw_found

echo [OK] 构建工具检查通过

:: 检查 Poppler 库
if exist "mingw64-poppler\include\poppler\cpp\poppler-document.h" (
    echo [OK] 找到 Poppler 库，PDF 功能可用
) else (
    echo [WARN]  未找到 Poppler 库，PDF 功能将不可用
)

:: 设置环境变量
if not "%CMAKE_PATH%"=="" set "PATH=%CMAKE_PATH%;%PATH%"
if not "%MINGW_PATH%"=="" set "PATH=%MINGW_PATH%;%PATH%"

:: 设置Qt路径环境变量
set "QT_ROOT=%QT_PATH%"
set "CMAKE_PREFIX_PATH=%QT_PATH%"

:: 清理构建目录和CMake缓存
echo.
echo 清理构建目录和CMake缓存...
if exist "build" rmdir /s /q "build"
if exist "CMakeCache.txt" del /q "CMakeCache.txt"
if exist "CMakeFiles" rmdir /s /q "CMakeFiles"
if exist "cmake_install.cmake" del /q "cmake_install.cmake"
if exist "Makefile" del /q "Makefile"
if exist "build.ninja" del /q "build.ninja"

:: 创建构建目录
echo 创建构建目录...
mkdir "build"
cd "build"

:: 配置项目 - 启用 Poppler 支持
echo.
echo 配置 CMake 项目...

:: 首先尝试使用 MinGW Makefiles
cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DUSE_POPPLER_LIBRARY=ON -DQT_ROOT="%QT_PATH%" -DCMAKE_PREFIX_PATH="%QT_PATH%" ..
if !errorlevel! neq 0 (
    echo [WARN]  MinGW Makefiles 配置失败，尝试不使用 Poppler 库...
    cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% -DUSE_POPPLER_LIBRARY=OFF -DQT_ROOT="%QT_PATH%" -DCMAKE_PREFIX_PATH="%QT_PATH%" ..
    if !errorlevel! neq 0 (
        echo [ERROR] CMake 配置失败
        cd ..
        exit /b 1
    )
)

echo [OK] CMake 配置成功

:: 编译项目
echo.
echo 开始编译项目...

:: 检查是否有 Makefile
if exist "Makefile" (
    echo 使用 MinGW Make 编译...
    if not "%MINGW_PATH%"=="" (
        "%MINGW_PATH%\mingw32-make.exe" -j4
    ) else (
        mingw32-make.exe -j4
    )
    if !errorlevel! neq 0 (
        echo [ERROR] MinGW Make 编译失败
        cd ..
        exit /b 1
    )
) else (
    echo [ERROR] 未找到 Makefile
    cd ..
    exit /b 1
)

echo [OK] 编译成功完成

cd ..

:: 构建完成
echo.
echo 构建完成！
echo ============================================
echo 构建类型: %CMAKE_BUILD_TYPE%
echo 可执行文件位置: build\bin\LimeReader.exe
echo.

